﻿using AutoMapper;
using MediatR;
using Reporting.API.Application.Commands.ProductionPlans;
using Reporting.API.DTOs.ProductionPlanDTO;
using Reporting.API.Utils;
using Reporting.Domain;
using Reporting.Errorcode.ErrorCode;
using Reporting.Infrastructure.Interfaces.ProductionPlans;
using Reporting.Infrastructure.Interfaces.Sourcetypes;
using System.Collections.Generic;

namespace Reporting.API.Application.CommandHandler.ProductionPlans
{
    public class SelectCompleteProductionPlanCommandHandler : IRequestHandler<SelectCompleteProductionPlanCommand, ApiResult<IList<SelectCompleteProductionPlanResponse>>>
    {
        private readonly IProductionPlanRepository _productionPlanRepository;
        private readonly ISourcetypeRepository _sourcetypeRepository;
        private readonly IMapper _mapper;
        public SelectCompleteProductionPlanCommandHandler(IProductionPlanRepository productionPlanRepository, ISourcetypeRepository sourcetypeRepository, IMapper mapper)
        {
            _productionPlanRepository = productionPlanRepository;
            _sourcetypeRepository = sourcetypeRepository;
            _mapper = mapper;
        }

        public async Task<ApiResult<IList<SelectCompleteProductionPlanResponse>>> Handle(SelectCompleteProductionPlanCommand request, CancellationToken cancellationToken)
        {
            try
            {
                // 获取所有生产计划
                var ProductionPlans = await _productionPlanRepository.GetAllAsync();

                var result=_mapper.Map<IList<SelectCompleteProductionPlanResponse>>(ProductionPlans);

                // 返回成功结果
                return ApiResult<IList<SelectCompleteProductionPlanResponse>>.Success(result, StatusCodeEnum.Success);
            }
            catch (Exception ex)
            {

                // 返回失败结果
                return ApiResult<IList<SelectCompleteProductionPlanResponse>>.Fail("查询失败：" + ex.Message, StatusCodeEnum.BadRequest);
            }
        }
    }
}
